package dao.hibernateImpl.greenlife;

import java.util.List;

import model.basic.BASICCATEGORY;
import model.basic.BASICCLICKBRIDGE;
import model.basic.BASICFILEBRIDGE;
import model.basic.BASICINFOBRIDGE;
import model.basic.BASICINFOMODEL;
import model.basic.BASICTOINDEXBRIDGE;
import model.basic.CLICK;
import model.basic.FILE;
import model.basic.INFOMATION;
import model.basic.TARGETINTERFACE;
import model.basic.TOINDEX;
import model.greenLife.basic.GREENLIFE;
import model.greenLife.file.GREENLIFEFILEBRIDGE;
import model.greenLife.info.GREENLIFEINFOBRIDGE;
import model.greenLife.toIndex.GREENLIFETOINDEXBRIDGE;
import model.greenLife.basic.SELF;
import model.greenLife.click.GREENLIFECLICKBRIDGE;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bsh.This;

import dao.CategoryDAO;
import dao.GREENLIFEDAO;
import dao.PHOTODAO;
import dao.helper.HibernateConfigurationHelper;
import dao.hibernateImpl.IndexHibernateImpl;
import dao.hibernateImpl.basic.CategoryHibernateImpl;
import dao.index.IndexDAO;

public class GREENLIFEHibernateImpl extends IndexHibernateImpl implements GREENLIFEDAO{

	private CategoryDAO CategoryDAO;
	
	

	GREENLIFE greenlife=new GREENLIFE();
	INFOMATION info=new INFOMATION();
	CLICK click=new CLICK();
	GREENLIFEINFOBRIDGE pib=new GREENLIFEINFOBRIDGE();
	GREENLIFETOINDEXBRIDGE ptib=new GREENLIFETOINDEXBRIDGE();
	TOINDEX toindex=new TOINDEX();
	GREENLIFECLICKBRIDGE pcb =new GREENLIFECLICKBRIDGE();
	BASICINFOMODEL bm=((BASICINFOMODEL)greenlife);
	FILE file=new FILE();
	GREENLIFEFILEBRIDGE pfb=new GREENLIFEFILEBRIDGE();
	dao.helper.HibernateConfigurationHelper hh=new HibernateConfigurationHelper();
	
	public CategoryDAO getCategoryDAO() {
		return CategoryDAO;
	}
	public void setCategoryDAO(CategoryDAO categoryDAO) {
		CategoryDAO = categoryDAO;
	}
	public List findTop7CategoryList(){
		
		BASICCATEGORY bc=new model.greenLife.category.GREENLIFECATEGORY();
		
		return CategoryDAO.findTop7CategoryList(bc);
	}


	public List findTop1To6GreenLifeByCateID(int CATEID){
		String sql="";
		
		sql=buildBasicSql(this.buildToIndexColumnSql().toString(), this.buildToIndexTableSql().toString(), buildWhereByToIndex(CATEID).toString(), buildOrderByPubTimeDesc().toString()).toString();
		this.setSql(sql);
		
		return this.findList(0, 6);
		
	}
	
	public StringBuffer buildToIndexColumnSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(hh.getTableName(greenlife.getClass()));
		sb.append(this._point);
		sb.append(greenlife.get_ID());
		sb.append(this._comma);
		sb.append(info.get_TITLE());
		sb.append(this._comma);
		sb.append(file.get_FILEADDR());
		return sb;
	}

	public StringBuffer buildOrderByPubTimeDesc(){
		StringBuffer sb=new StringBuffer();
		sb.append(this._orderby);
		sb.append(this._space);
		sb.append(info.get_PUBTIME());
		return sb;
	}
	public StringBuffer buildToIndexTableSql(){
		StringBuffer sb=new StringBuffer();
		sb.append(selfTable());
		sb.append(buildInfoRalationsSql());
		sb.append(buildToIndexRalationsSql());
		sb.append(buildFileRalationsSql());
		return sb;
	}
	public StringBuffer buildWhereByToIndex(int CATEID){
		StringBuffer sb=new StringBuffer();
		sb=buildWhereByCondi(toindex.get_UPTOINDEX(),CATEID);
		return sb;	
	}
	public StringBuffer buildWhereByCondi(String condiname,int CATEID){
		
		StringBuffer sb=new StringBuffer();
		sb.append(this.where);
		sb.append(this._space);
		sb.append(condiname);
		sb.append(this._eq);
		sb.append(2);
		if(CATEID!=0){
			sb.append(this._and);
			sb.append(greenlife.get_CATEGORYID());
			sb.append(this._eq);
			sb.append(CATEID);
		}
		return sb;	
	}
	public StringBuffer selfTable(){
		StringBuffer sb=new StringBuffer();
		String selftb=hh.getTableName(greenlife.getClass());
		sb.append(selftb);
		
		
		return sb;
	}

	public StringBuffer buildInfoRalationsSql(){
		
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICINFOBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(pib.getClass());
		String selftb=hh.getTableName(greenlife.getClass());
		String targettb=hh.getTableName(info.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		
		
		return sb;
		
	}
	public StringBuffer buildToIndexRalationsSql(){
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICTOINDEXBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(ptib.getClass());
		String selftb=hh.getTableName(greenlife.getClass());
		String targettb=hh.getTableName(toindex.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		return sb;
		
	}

	public StringBuffer buildFileRalationsSql(){
		StringBuffer sb=new StringBuffer();
		TARGETINTERFACE ti=new BASICFILEBRIDGE();
		String bmID=bm.get_ID();
		String ralationtb=hh.getTableName(pfb.getClass());
		String selftb=hh.getTableName(greenlife.getClass());
		String targettb=hh.getTableName(file.getClass());
		sb=buildRalationSql(ti,bmID,ralationtb,selftb,targettb,SELF._SELFID,"");
		
		return sb;
		
	}
	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		GREENLIFEDAO ch=(GREENLIFEDAO)ac.getBean("GREENLIFEDAO");
//		BASICCATEGORY bc=new model.video.category.VIDEOCATEGORY();
		System.out.println(((Object [])(ch.findTop1To6GreenLifeByCateID(1).get(0)))[0]);
//		StringBuilder sb=ch.buildToIndexRalationsSql(new StringBuilder());
//		System.out.println(sb.toString());
	}
}
